/**
 * Created by mac on 14-5-29.
 */
var localData = {Rows:[
    {"name":"麦太","sex":"女","phone":"13833333333","address":"香港九龙广华XXX路"}
]};
$(initGrid);
var grid = null;
function initGrid() {
    grid = $("#maingrid").ligerGrid({
        columns: [
            {display: '家长姓名',name: 'name', width: '10%'},
            {display: '性别',name: 'sex', width: '10%'},
            {display: '联系方式',name: 'phone',width: '20%'},
            {display: '联系地址',name: 'address', width: '49%'},
            {display: '操作', isSort: false, width: '9%',render: function(row, rowindex) {
                var h = "";
                h += "<a href='javascript:updateParent(\"" + row.name + "\")'>修改</a> ";
                h += '<a href="javascript:delParent(' + row.id + ')">删除</a>';

                return h;
            }
        }],
        checkbox: true, enabledEdit: true,clickToEdit: false,selectRowButtonOnly: true,rownumbers:false,
        data:localData,
        //dataAction: 'server',
       // url: '@{system.SellerAccounts.getSellerAccountData()}',
        sortname: 'createdTime',usePager: 'true',pageSize: 50, width: '100%',height:'100%',
        onSelectRow: function(row, rowindex) {
            $("#txtrowindex").val(rowindex);
        },
        onAfterShowData: function(grid) {
            if ($.fn.ligerCheckBox) $(".l-grid-body input:checkbox,.l-grid-hd-cell input:checkbox", grid).ligerCheckBox({
                css: {
                    marginTop: 3
                }
            });
        }
    });
}

//添加学校
var addParent = function(){
     $.ligerDialog.open({
        title:"添加家长信息",
        url: 'addParent.html',
        width: "400",height:'220',
        showMin: false,showMax: true, showToggle: false,isLock:true,
        isDrag: true, isResize: true, modal: true,
        buttons: [
            { text: '保存', onclick: function (item, dialog) {
                alert(item.text);

                //发送请求成功保存后，刷新表格数据
                grid.loadData();
            }},
            { text: '取消', onclick: function (item, dialog) { dialog.close(); }}
        ]
     });
};

//删除学校
var delParent = function(rowid){
   $.ligerDialog.confirm('<span style="position:absolute;margin-top: 10px;">确认删除该条数据吗？</span>', function (yes) {
       //发送请求后台删除数据移除表格该条数据
       if(yes){
          //如果后台删除数据请求成功，则删除选中数据
          grid.deleteRow(0);
       }
   });
};

//批量删除学校
var delBatchParent = function(){
    //获取选中数据
    var rows = grid.getSelectedRows();
    if(rows.length < 1){
        parent.$.showShortMessage({msg:"请最少选择一条数据",animate:false});
        return false;
    }
    $.ligerDialog.confirm('<span style="position:absolute;margin-top: 10px;">确认删除这些数据吗？</span>', function (yes) {
       //发送请求后台删除数据移除表格该条数据
       if(yes){
           //如果后台删除数据请求成功，则删除选中数据
           grid.deleteSelectedRow();
       }
   });
};

//修改家长信息
var updateParent = function(name){
    //真正使用的时候，这里传该条家长信息在数据库ID，参数传给修改页面，进行查询。
     $.ligerDialog.open({
        title:"修改家长信息",
        url: 'addParent.html?name='+name,
        width: "400",height:'220',
        showMin: false,showMax: true, showToggle: false,isLock:true,
        isDrag: true, isResize: true, modal: true,
        buttons: [
            { text: '保存', onclick: function (item, dialog) {
                alert(item.text);

                //发送请求成功保存后，刷新表格数据
                grid.loadData();
            }},
            { text: '取消', onclick: function (item, dialog) { dialog.close(); }}
        ]
     });
};

